package com.hl.controller.huiyuan;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.hl.service.ILiliShopMemberService;
import com.hl.service.impl.LiliShopMemberServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hl.domain.LiliShopMember;
import com.hl.service.HuishouMemberService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 会员列表Controller
 * 
 * @author ruoyi
 * @date 2025-08-05
 */
@RestController
@RequestMapping("/member")
public class LiliShopMemberController extends BaseController
{
    @Autowired
    private ILiliShopMemberService liliShopMemberService;

    /**
     * 查询会员列表列表
     */
    @RequiresPermissions("huiyuan:member:list")
    @GetMapping("/list")
    public TableDataInfo list(LiliShopMember liliShopMember)
    {

        startPage();
        List<LiliShopMember> list = liliShopMemberService.selectLiliShopMemberList(liliShopMember);
        return getDataTable(list);
    }

    /**
     * 导出会员列表列表
     */
    @RequiresPermissions("huiyuan:member:export")
    @Log(title = "会员列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LiliShopMember liliShopMember)
    {
        List<LiliShopMember> list = liliShopMemberService.selectLiliShopMemberList(liliShopMember);
        ExcelUtil<LiliShopMember> util = new ExcelUtil<LiliShopMember>(LiliShopMember.class);
        util.exportExcel(response, list, "会员列表数据");
    }

    /**
     * 获取会员列表详细信息
     */
    @RequiresPermissions("huiyuan:member:query")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") String memberId)
    {
        return success(liliShopMemberService.selectLiliShopMemberByMemberId(memberId));
    }

    /**
     * 新增会员列表
     */
    @RequiresPermissions("huiyuan:member:add")
    @Log(title = "会员列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiliShopMember liliShopMember)
    {
        if (
                liliShopMember.getMemberUuid()==null||liliShopMember.getMemberUuid()==""
                &liliShopMember.getRegisterTime()==null
        ){
            return toAjax(liliShopMemberService.insertLiliShopMember(liliShopMember));
        }else {
            return AjaxResult.error("格式错误，请重试");
        }

    }

    /**
     * 用户注册接口（无需权限验证）
     */
    @Log(title = "用户注册", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    public AjaxResult register(@RequestBody LiliShopMember liliShopMember)
    {
        try {
            // 检查用户名是否已存在
            LiliShopMember existingMember = liliShopMemberService.selectLiliShopMemberByMemberName(liliShopMember.getMemberName());
            if (existingMember != null) {
                return AjaxResult.error("用户名已存在，请选择其他用户名");
            }
            
            // 检查手机号是否已存在
            if (liliShopMember.getContact() != null && !liliShopMember.getContact().trim().isEmpty()) {
                LiliShopMember memberQuery = new LiliShopMember();
                memberQuery.setContact(liliShopMember.getContact());
                List<LiliShopMember> existingMembers = liliShopMemberService.selectLiliShopMemberList(memberQuery);
                if (existingMembers != null && !existingMembers.isEmpty()) {
                    return AjaxResult.error("手机号已被注册，请使用其他手机号");
                }
            }
            
            // 设置默认值
            liliShopMember.setMemberUuid(null); // 让Service自动生成UUID
            liliShopMember.setRegisterTime(null); // 让Service自动设置注册时间
            liliShopMember.setRoleId(2); // 默认用户角色
            liliShopMember.setGender("unknown"); // 默认性别未知
            liliShopMember.setIsEnabled(1); // 默认启用
            liliShopMember.setPoints(0L); // 默认积分0
            
            int result = liliShopMemberService.insertLiliShopMember(liliShopMember);
            if (result > 0) {
                return AjaxResult.success("注册成功");
            } else {
                return AjaxResult.error("注册失败，请重试");
            }
        } catch (Exception e) {
            System.err.println("注册异常: " + e.getMessage());
            return AjaxResult.error("注册失败: " + e.getMessage());
        }
    }

    /**
     * 修改会员列表
     */
    @RequiresPermissions("huiyuan:member:edit")
    @Log(title = "会员列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiliShopMember liliShopMember)
    {
        System.out.println(liliShopMember+"接收参数");
        return toAjax(liliShopMemberService.updateLiliShopMember(liliShopMember));
    }

    /**
     * 删除会员列表
     */
    @RequiresPermissions("huiyuan:member:remove")
    @Log(title = "会员列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable String[] memberIds)
    {
        return toAjax(liliShopMemberService.deleteLiliShopMemberByMemberIds(memberIds));
    }
}
